#pragma once
#include "util.hpp"

namespace cloud
{
    class Config
    {
    public:
        static Config& getInstance()
        {
            static Config root; // 由C++11支持
            return root;
        }

        size_t getHotTime() { return _hot_time; }
        size_t getServerPort() { return _server_port; }
        std::string getServerIp() { return _server_ip; }
        std::string getDownloadPrefix() { return _download_prefix; }
        std::string getPackfileSuffix() { return _packfile_suffix; }
        std::string getBackDir() { return _back_dir; }
        std::string getPackDir() { return _pack_dir; }
        std::string getBackupFile() { return _backup_file; }
        
    private:
        Config(){ readConfigFile(); }
        Config(const Config &) = delete;
        Config operator=(const Config &) = delete;
        void readConfigFile()
        {
            std::string str;
            Json::Value root;
            UtilFile file("/home/ubuntu/cloud-backup/cloud_server/cloud.conf");

            if(file.getContent(str) == false)
            {
                std::cout << "read get config file content fail!" << std::endl;
                exit(-1);
            }
            
            if(JosnUtil::unserialize(str, root) == false)
            {
                std::cout << "unserialize config file fail!" << std::endl;
                exit(-1);
            }
            
            _hot_time = root["hot_time"].asInt();
            _server_port = root["server_port"].asInt();
            _server_ip = root["server_ip"].asString();
            _download_prefix = root["download_prefix"].asString();
            _packfile_suffix = root["packfile_suffix"].asString();
            _back_dir = root["back_dir"].asString();
            _pack_dir = root["pack_dir"].asString();
            _backup_file = root["backup_file"].asString();
        }
    private:
        size_t _hot_time;            // 热点判断时间
        size_t _server_port;         // 服务器访问端口
        std::string _server_ip;      // 服务器访问 IP 地址
        std::string _download_prefix;// 文件下载URL前缀路径
        std::string _packfile_suffix;// 压缩包后缀名称
        std::string _back_dir;       // 备份文件存放目录
        std::string _pack_dir;       // 压缩包存放目录
        std::string _backup_file;    // 数据信息存放文件
    };
}